package com.customization.unionpay.market.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.service.MarketingDailyService;
import com.customization.unionpay.market.util.FormDataUtil;
import com.customization.unionpay.market.util.FormUtil;
import weaver.backup.logging.Logger;
import weaver.backup.logging.LoggerFactory;
import weaver.conn.RecordSet;

import java.util.ArrayList;
import java.util.List;

/**
 * 营销日报表
 */
public class MarkertingDailyServiceImpl implements MarketingDailyService {
    private static Logger log = LoggerFactory.getLogger(MarkertingDailyServiceImpl.class);


    @Override
    public Boolean updateMarketingDailyZt(String zt, String sqm) {
        List<JSONObject> item = FormDataUtil.getSelectItem(com.customization.unionpay.market.util.FormUtil.UF_YXRBBTZ_ID,"zt",zt);
        if(CollectionUtil.isEmpty(item)){
            log.error("未查询到对应枚举值");
            return false;
        }
        RecordSet rs = new RecordSet();
        StringBuilder  sql = new StringBuilder();
        sql.append("update ").append(FormUtil.UF_YXRBBTZ).append(" set zt = ").append(item.get(0).getString("value"))
                .append(" where yxpthdsqm = '").append(sqm).append("'");
       try {
          return rs.execute(sql.toString());
       }catch (Exception e){
           log.error("修改字段值报错");
       }
        return false;
    }

    @Override
    public List<JSONObject> queryBySqm(List<String> sqmList) {
        if(CollectionUtil.isEmpty(sqmList)){
            log.error("查询的授权码条件为空");
            return null;
        }
        StringBuilder querySql = new StringBuilder();
        querySql.append("select id,yxpthdsqm,zt,rblslx from uf_yxrbbtz where yxpthdsqm in (");
        sqmList.forEach(o->{
            querySql.append(o).append(",");
        });
        querySql.deleteCharAt(querySql.length()-1).append(")");
        RecordSet rs = new RecordSet();
        List<JSONObject> resultList = new ArrayList<>();
        try {
            rs.execute(querySql.toString());
            while (rs.next()){
                JSONObject o = new JSONObject();
                o.put("id",rs.getInt("id"));
                o.put("yxpthdsqm",rs.getString("yxpthdsqm"));
                o.put("zt",rs.getInt("zt"));
                o.put("rblslx",rs.getInt("rblslx"));
                resultList.add(o);
            }
            return resultList;
        }catch (Exception e){
            log.error("根据授权码查询列表值为空");
        }

        return null;
    }
}
